/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dataBasePlaylisted;

import controlPlaylisted.ControlPlaylistCommand;
import Request.RequestPlaylist;
import Response.ResponsePlaylist;
import dataPlaylisted.Music;
import dataPlaylisted.Playlist;


import java.beans.XMLDecoder;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

import java.io.IOException;


import java.util.ArrayList;

/**
 *
 * @author Administrador
 */
public class getPlaylistListCommand implements ControlPlaylistCommand{

    public void execute(RequestPlaylist _req, ResponsePlaylist _resp){

        String file_name = "PlaylistModel.xml";
        FileInputStream m_fis = null;
        ObjectInputStream m_in = null;
        XMLDecoder m_xdec;

        try{
            m_fis = new FileInputStream(file_name);
        }catch(IOException e){
        }

//        try{
//            m_in = new ObjectInputStream(m_fis);
//        }catch(IOException e){
//        }

        m_xdec = new XMLDecoder(m_fis);

        ArrayList<Playlist> list;

        list = new ArrayList<Playlist>();

        ArrayList<String> str_list;
        str_list = new ArrayList<String>();

        try {
            str_list = (ArrayList<String>) m_xdec.readObject();
        } catch (Exception e){
        }

        if(str_list != null){
           
            int i = 0;
            while (i < str_list.size()){
                Playlist _playlist;
                _playlist = new Playlist();

                _playlist.setId(_playlist.toInt(str_list.get(i)));
                i++;

                _playlist.setUserId(_playlist.toInt(str_list.get(i)));
                i++;

                _playlist.setMusicNum(_playlist.toInt(str_list.get(i)));
                i++;
              
                int j = 0;
                while (j < _playlist.getMusicNum()){
                    Music _music;
                    _music = new Music();
                    
                    _music.setTrackName(str_list.get(i));
                    i++;
                    _music.setArtistName(str_list.get(i));
                    i++;
                    _music.setStyle(str_list.get(i));
                    i++;
                    _music.setCammelot(str_list.get(i));
                    i++;
                    _playlist.setPlaylistElement(_music);
                    j++;
                }
                list.add(_playlist);
            }           
        }       
        _resp.setObjectList(list);
    }
}
